<template>
  <div class="stack-chart">
    <div class="title">{{ stackTitle }}</div>
    <div class="chart" ref="stackChartDom"></div>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, onMounted, toRefs, watch, nextTick } from 'vue'
const stackChartDom = ref(null)
const props = defineProps({
  stackData: Object,
  stackTitle: String
})
const { proxy } = getCurrentInstance()
const option = reactive({
  tooltip: {
    trigger: 'axis',
    backgroundColor: '#666',
    borderColor: '#666',
    textStyle: {
      color: '#fff'
    },
    axisPointer: {
      type: 'none'
    }
  },
  legend: {
    data: [],
    textStyle: {
      color: '#fff'
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: [],
    axisLabel: {
      show: true,
      color: '#ffffff'
    }
  },
  yAxis: {
    type: 'value',
    axisLabel: {
      show: true,
      color: '#ffffff'
    }
  },
  series: []
})
let brokenChart = null
const initChart = () => {
  brokenChart = proxy.$echarts.init(stackChartDom.value)
  updateOption(option)
}

const updateOption = (option) => {
  const { stackData } = toRefs(props)
  console.log('stackData', stackData.value)
  option.color = stackData.value?.color
  option.xAxis.data = stackData.value?.xAxisData
  option.legend.data = stackData.value?.legendData
  option.series = stackData.value?.seriesData
  brokenChart?.setOption(option)
}
watch(
  () => props.stackData,
  () => {
    updateOption(option)
  }
)
onMounted(() => {
  nextTick(() => {
    initChart()
  })
})
</script>

<style lang="less" scoped>
.stack-chart {
  width: 100%;
  height: 300px;
  color: #fff;
  background: url('@/assets/image/chart-bg.png') no-repeat;
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  .title {
    height: 40px;
    line-height: 40px;
    font-size: 18px;
    font-weight: 650;
    color: #01e4f8;
    text-align: center;
  }
  .chart {
    flex: 1;
  }
}
</style>
